package java01;

import java.util.Scanner;

public class StackTraceTest {

    private static int f(int n){
        System.out.println("f("+n+")");
        Throwable t = new Throwable();
        StackTraceElement[] frames = t.getStackTrace();
        for(StackTraceElement f:frames){
            System.out.println(f+"     ");//打印递归阶乘函数堆栈情况
            System.out.print(f.getFileName()+"     ");
            System.out.print(f.getLineNumber()+"     ");
            System.out.print(f.getClassName()+"     ");
            System.out.print(f.getMethodName()+"     ");
            System.out.println(f.isNativeMethod());
        }
        int r;
        if(n<=1){
            r=1;
        }else{
            r=n*f(n-1);
        }
        System.out.println("return" +r);
        return r;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter n:");
        int n = in.nextInt();
        f(n);
        //捕获异常所花费的时间大大超出为空判定，所以使用异常的规则是：只在异常情况下使用异常机制
        //检测错误时，“苛刻”比放任更好。需要传递异常给高层次方法通知用户发生了错误。“早抛出，晚捕获”
    }
}
